package com.tbit.main.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.OperaPswService;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.manager.util.StringUtils;
import com.tbit.manager.api.TransactionOrderStatApi;
import com.tbit.manager.api.dto.DepositRefundPageDTO;
import com.tbit.manager.api.dto.OrderRefundDTO;
import com.tbit.manager.api.dto.TransactionOrderIncomeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 商户交易订单统计
 *
 * @AUTHOR: zhangtianyou
 * @DATE: 2024-9-13
 **/
@RequestMapping("/transactionOrderStat")
@RestController
public class TransactionOrderStatController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TransactionOrderStatApi transactionOrderStatApi;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 查询商户交易订单分页（调用管理端接口）
     *
     * @param transactionOrderIncomeDTO
     * @return
     */
    @GetMapping("/getIncomePageApp")
    public Result<PageResult<TransactionOrderIncomeDTO>> getIncomePageApp(@Valid TransactionOrderIncomeDTO transactionOrderIncomeDTO) {
        //获取用户
        AccountUser accountUser = tokenService.getAccount(transactionOrderIncomeDTO.getToken());
        //获取品牌
        Integer brandId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();
        transactionOrderIncomeDTO.setBrandId(brandId);
        Result<PageResult<TransactionOrderIncomeDTO>> incomePageApp = transactionOrderStatApi.getIncomePageApp(transactionOrderIncomeDTO);
        convertCode(incomePageApp);
        return incomePageApp;
    }

    /**
     * 根据订单编号查询,订单是否已开具发票（调用管理端接口）
     *
     * @param orderNo 订单编号
     * @return
     */
    @GetMapping("/queryOrderHasBeenInvoicedByOrderNoApp")
    public Result<Boolean> getIncomePageApp(String orderNo) {
        if (StringUtils.isEmpty(orderNo)){
            return Result.errorMessage("订单编号不能为空", 0, null);
        }
        Result<Boolean> booleanResult = transactionOrderStatApi.queryOrderHasBeenInvoicedByOrderNoApp(orderNo);
        convertCode(booleanResult);
        return booleanResult;
    }

    /**
     * 押金退款实体类（调用管理端接口）
     *
     * @param depositRefundPageDTO  押金退款实体
     * @return Result<JSONObject>
     */
    @GetMapping("/depositRefundApp")
    public Result<Integer> depositRefundApp(@Valid DepositRefundPageDTO depositRefundPageDTO) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(depositRefundPageDTO.getPsw())) {
            operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(),
                    depositRefundPageDTO.getPsw());
        }

        depositRefundPageDTO.setAccountUserId(LoginUserContextHolder.getLoginUserId());
        Result<Integer> integerResult = transactionOrderStatApi.depositRefundApp(depositRefundPageDTO);
        convertCode(integerResult);
        return integerResult;
    }

    /**
     * 骑行卡、免押卡、会员卡、优惠券套餐退款（调用管理端接口）
     *
     * @param orderRefundDTO  退款参数
     * @return Result<JSONObject>
     */
    @GetMapping("/orderRefundApp")
    public Result<Integer> orderRefundApp(@Valid OrderRefundDTO orderRefundDTO) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(orderRefundDTO.getPsw())) {
            operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(),
                    orderRefundDTO.getPsw());
        }

        orderRefundDTO.setAccountUserId(LoginUserContextHolder.getLoginUserId());
        Result<Integer> integerResult = transactionOrderStatApi.orderRefundApp(orderRefundDTO);
        convertCode(integerResult);
        return integerResult;
    }

    /**
     * 转换状态码
     * @param result 返回值
     */
    private static void convertCode(Result<?> result) {
        if (result.getCode() == 200) {
            result.setCode(1);
        }else {
            result.setCode(0);
        }
    }
}